﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class IDAndTime
{
    public int id;
    public float time;
    public bool startAttcak;
}


public class AttackControl : MonoBehaviour
{
    //敌方召唤物
    public List<SummonAnimal> enemySummonList=new List<SummonAnimal>();
    //己方召唤物
    public List<SummonAnimal> MySummonlist=new List<SummonAnimal>();

    //每个怪物id对应的攻击倒计时
    public List<IDAndTime> IDToTime=new List<IDAndTime>();
    //怪物id对应怪物属性
    public Dictionary<int,SummonAnimal> IDToSummonAnimals=new Dictionary<int, SummonAnimal>();
    //移動控制id,對應的
    public Dictionary<int, SummonAnimal> moveOBjs=new Dictionary<int, SummonAnimal>();
    public bool endAttack;
	// Use this for initialization
	void Start ()
	{
        //设定攻击目标
	    SetAttcakAim();
	}

    private void SetAttackTime(SummonAnimal summ)
    {
        IDAndTime idAndTime=new IDAndTime();
        idAndTime.id = summ.ID;
        idAndTime.time = summ.attackInfo.AttackSpeed;
        idAndTime.startAttcak = false;
        IDToTime.Insert(IDToTime.Count,idAndTime);
    }

    //为list里的怪物设定攻击目标
    private void SetAttcakAim()
    {
        foreach (var summ in MySummonlist)
        {
            SetAttackTime(summ);
            IDToSummonAnimals.Add(summ.ID, summ);

            summ.attackInfo.AttackObject = NearSummonAnimal(summ, enemySummonList);
        }
        foreach (var summ in enemySummonList)
        {
            SetAttackTime(summ);
            IDToSummonAnimals.Add(summ.ID, summ);
            summ.attackInfo.AttackObject = NearSummonAnimal(summ,MySummonlist);
        }
    }

    //找到最近的攻击目标
    private SummonAnimal NearSummonAnimal(SummonAnimal summon ,List<SummonAnimal> summonList)
    {
        float minDistance = -1f;
        int number = 0;
        Vector2 pos1 = new Vector2(summon.postion.x, summon.postion.z);
        for (int i = 0; i < summonList.Count; i++)
        {
            Vector2 pos2 = new Vector2(summonList[i].postion.x, summonList[i].postion.z);
            float distance = Vector3.Distance(pos1, pos2);
            if (distance < minDistance || minDistance < 0)
            {
                minDistance = distance;
                number = i;
            }
        }
        Debug.Log(minDistance);
        return summonList[number];
    }

    //攻击或者移动
    public void AttackAndMove(List<SummonAnimal> summonList,List<SummonAnimal> otherSummonList)
    {
        foreach (var summ in summonList)
        {
            //判断目标被消灭后的逻辑
            if (summ.attackInfo.AttackObject == null)
            {
                //如果另外list还有怪物则更新攻击目标，没有则停止
                if (otherSummonList != null)
                {
                    summ.attackInfo.AttackObject = NearSummonAnimal(summ, otherSummonList);
                }
                else
                {
                    StateChange(summ, DefaultConst.state.idle);
                    endAttack = true;
                    return;
                }
            }
            if (summ.attackInfo.InAttackDistance(summ)&& summ.myState != DefaultConst.state.attack)
            {
                StateChange(summ, DefaultConst.state.attack);
                for (int i = 0; i < IDToTime.Count; i++)
                {
                    if (IDToTime[i].id == summ.ID)
                    {
                        IDToTime[i].startAttcak = true;
                    }
                }
            }
            else
            {   
                StateChange(summ,DefaultConst.state.walk);
            }
        }
    }

    //判断状态有没有改变，有就播放动画
    private void StateChange(SummonAnimal summ,DefaultConst.state currentstate)
    {
        if (summ.myState == currentstate)
        {
            return;
        }
        else
        {
            summ.myState = currentstate;
            //动画切换
            if (currentstate == DefaultConst.state.walk)
            {
                //向目标移动
                moveOBjs.Add(summ.ID,summ);
                Debug.Log("setmove!!!!!!");
            }
            else
            {
                if (moveOBjs.ContainsKey(summ.ID))
                {
                    moveOBjs.Remove(summ.ID);
                }     
            }
        }
    }
    //向目標移動
    public void MoveToTarget(GameObject selfobj, GameObject target,float speed)
    {
        if (selfobj == null || target == null)
        {
            return;
        }
        Transform pos = selfobj.transform;
        Vector3 targetpos = new Vector3(target.transform.localPosition.x, pos.position.y, target.transform.localPosition.z);

        pos.position = Vector3.MoveTowards(pos.position, targetpos, speed);
        //Debug.Log("selfobj:"+selfobj.name+pos);
    }


    // Update is called once per frame
    void Update () {
	    AttackAndMove(MySummonlist,enemySummonList);
        AttackAndMove(enemySummonList,MySummonlist);

        foreach (KeyValuePair<int, SummonAnimal> kvp in moveOBjs)
        {
            MoveToTarget(kvp.Value.gameObject, kvp.Value.attackInfo.AttackObject.gameObject, kvp.Value.speed*Time.deltaTime);
        }

        for (int i = 0; i < IDToTime.Count; i++)
	    {
	        if (IDToTime[i].startAttcak)
	        {
	            IDToTime[i].time -= Time.deltaTime;
            }
	        if (IDToTime[i].time < 0)
	        {
	            SummonAnimal outSummmonAnimal;
	            IDToSummonAnimals.TryGetValue(IDToTime[i].id, out outSummmonAnimal);
	            outSummmonAnimal.attackInfo.DoDamage();
	            SummonAnimal attackObj = outSummmonAnimal.attackInfo.AttackObject;
                Debug.Log("attackObj:"+ attackObj+"...attackObjHP:"+ attackObj.HP);
                if (attackObj.HP < 0)//被攻击者死亡则停止攻击
	            {
	                if (enemySummonList.Contains(attackObj))
	                {
	                    enemySummonList.Remove(attackObj);
	                }
	                if (MySummonlist.Contains(attackObj))
	                {
	                    MySummonlist.Remove(attackObj);
	                }
	                attackObj.gameObject.SetActive(false);
	                IDToTime[i].startAttcak = false;
	            }
                else
                {
                    IDToTime[i].time = outSummmonAnimal.attackInfo.AttackSpeed;
                }
	        }
	    }
    }
}
